package by.amakarevich.webapp.page.shoppingcart;

import java.util.List;

import javax.inject.Inject;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.ResourceModel;

import by.amakarevich.datamodel.Product;
import by.amakarevich.datamodel.ShoppingCart;
import by.amakarevich.datamodel.ShoppingCartInfo;
import by.amakarevich.datamodel.UserAccount;
import by.amakarevich.services.ShoppingCartInfoService;
import by.amakarevich.services.ShoppingCartService;
import by.amakarevich.webapp.app.BasicAuthenticationSession;
import by.amakarevich.webapp.page.AlternativeBaseLayout;
import by.amakarevich.webapp.page.address.edit.AddressCreateEditPage;
import by.amakarevich.webapp.page.shoppingcart.panel.ShoppingCartInfoPanel;

public class ShoppingCartPage extends AlternativeBaseLayout {
	
	@Inject
	private ShoppingCartService shoppingCartService;
	
	@Inject
	private ShoppingCartInfoService shoppingCartInfoService;
	
	private Long totalPrice;
	private Product product;
	
	UserAccount user = BasicAuthenticationSession.get().getUser();
	ShoppingCart shoppingCart =shoppingCartService.getByUser(user) ;
	
	@Override
	protected void onInitialize() {
		super.onInitialize();
		
		
		final List<ShoppingCartInfo> allShoppingCartInfo = shoppingCartInfoService.getAllByShoppingCart(shoppingCart);
		
		PriceValidation(allShoppingCartInfo);
		
		add(new ListView<ShoppingCartInfo>("details-panel",allShoppingCartInfo) {
			@Override
			protected void populateItem(ListItem<ShoppingCartInfo> item) {
				ShoppingCartInfo shoppingCartInfo = item.getModelObject();
				item.add(new Label("id", shoppingCartInfo.getId()).setVisible(false));
				item.add((new ShoppingCartInfoPanel("shopping-cart-info-panel", shoppingCartInfo)));
				
			}
		});
		
		
		add(new Label("totalPrice", new Model<Long>(totalPrice)));
		
		add(new Link("Address-create-page-link") {
			@Override
			protected void onConfigure() {
				super.onConfigure();
				
				setVisible(allShoppingCartInfo.size()>0);
			}
			
			@Override
			public void onClick() {
				setResponsePage(new AddressCreateEditPage(user,shoppingCart));
			}
		});

	}
	
	private Long PriceValidation(List<ShoppingCartInfo> allShoppingCartInfo){
		totalPrice=0l;
		for (ShoppingCartInfo shoppingCartInfoPrice : allShoppingCartInfo) {
			shoppingCartInfoPrice = shoppingCartInfoService.getByShoppingCartId(shoppingCartInfoPrice.getId());
			product = shoppingCartInfoPrice.getProduct();
			if(shoppingCartInfoPrice.getPrice()!=product.getPrice()){
				shoppingCartInfoPrice.setPrice(product.getPrice());
				shoppingCartInfoService.updateShoppingCartInfo(shoppingCartInfoPrice);
			}
			totalPrice= totalPrice + shoppingCartInfoPrice.getPrice()*shoppingCartInfoPrice.getQuantity();
		}
		
		if(shoppingCart.getTotalPrice()!=totalPrice){
			shoppingCart.setTotalPrice(totalPrice);
			shoppingCartService.updateShoppingCart(shoppingCart);
		}
		return totalPrice;
	}
	
	@Override
	protected IModel<String> getPageTitle() {
		return new ResourceModel("p.shoppingcart.title");
	}
			
		
}
